Note that positive values in deficit_pc mean budget surpluses and negative deficits Uploading data
df_deficit_pct <- read_csv("data/deficit_pct_gdp_OECD.csv")
Creating a wide-form dataset with columns for each year There are 23 rows with flag E (estimate/preliminary data) and 1 B (break in series) that were put in table df_e_or_b
df_deficit_pct %>% count(`Flag Codes`)
## # A tibble: 3 x 2
## `Flag Codes` n
## <chr> <int>
## 1 B 1
## 2 E 23
## 3 <NA> 1116
df_deficit_pct %>% filter(!is.na(`Flag Codes`))
## # A tibble: 24 x 8
## LOCATION INDICATOR SUBJECT MEASURE FREQUENCY TIME deficit_pc `Flag Codes`
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <chr>
## 1 FIN GGNLEND TOT PC_GDP A 1975 4.93 E
## 2 FIN GGNLEND TOT PC_GDP A 1976 7.61 E
## 3 FIN GGNLEND TOT PC_GDP A 1977 6.25 E
## 4 FIN GGNLEND TOT PC_GDP A 1978 3.83 E
## 5 FIN GGNLEND TOT PC_GDP A 1979 3.40 E
## 6 FRA GGNLEND TOT PC_GDP A 2018 -2.29 E
## 7 FRA GGNLEND TOT PC_GDP A 2019 -3.01 E
## 8 GRC GGNLEND TOT PC_GDP A 2010 -11.3 B
## 9 GRC GGNLEND TOT PC_GDP A 2018 1.02 E
## 10 GRC GGNLEND TOT PC_GDP A 2019 1.52 E
## # … with 14 more rows
df_wide_deficit <- df_deficit_pct %>%
select(-`Flag Codes`) %>% #this removes the variable Flag Codes to make the wide dataset cleaner
pivot_wider(names_from = TIME, values_from = deficit_pc)
Visualising balance as % of GDP for all countries over the years (positive values are surpluses, negative values are deficits)
p <- df_deficit_pct %>% #making p into a ggplot object to visualise w/ plotly
# line below would invert surplus/deficit in deficit_pc, making higher values mean higher deficits
# mutate(deficit_pc=-deficit_pc) %>%
ggplot(aes(TIME, deficit_pc, colour=LOCATION)) +
geom_line()
ggplotly(p) #plotly lets me see the legend when scrolling through the plot
Creating rolling averages over 5, 7 and 10 years
df_deficit_pct <- df_deficit_pct %>% #this function uses longform
group_by(LOCATION) %>%
mutate(bal_05ya = rollmean(deficit_pc, k = 5, fill = NA),
bal_07ya = rollmean(deficit_pc, k = 7, fill = NA),
bal_10ya = rollmean(deficit_pc, k = 10, fill = NA)) %>%
ungroup() #this closes that group_by(LOCATION)
Visualising 5 year rolling averages of balance as % of GDP for all countries
p1 <- df_deficit_pct %>%
ggplot(aes(TIME, bal_05ya, colour=LOCATION)) +
geom_line()
ggplotly(p1)
Visualising 7 year rolling averages of balance as % of GDP for all countries
p2 <- df_deficit_pct %>%
ggplot(aes(TIME, bal_07ya, colour=LOCATION)) +
geom_line()
ggplotly(p2)
Visualising 10 year rolling averages of balance as % of GDP for all countries
p3 <- df_deficit_pct %>%
ggplot(aes(TIME, bal_10ya, colour=LOCATION)) +
geom_line()
ggplotly(p3)
Visualising 7 year rolling averages of balance as % of GDP for all countries from 2000
p4 <- df_deficit_pct %>%
filter(TIME>=2000) %>%
ggplot(aes(TIME, bal_07ya, colour=LOCATION)) +
geom_line()
ggplotly(p4)